package com.xiezc.springboot_dht.model;

import com.xiezc.springboot_dht.util.Const;

import java.util.Objects;

import static com.xiezc.springboot_dht.model.BeType.number;

/**
 * @author xzc
 */
public class BeLong extends BeObject<Long> {

    public BeLong(Long val) {
        this.value = val;
    }

    @Override
    public byte[] getContent() {
        if (this.content != null) {
            return this.content;
        }

        byte[] bytes = Objects.toString(this.value).getBytes(Const.defaultCharset);
        content = new byte[bytes.length + 2];
        content[0] = number.getPre();
        content[bytes.length + 1] = number.getSuffix();
        System.arraycopy(bytes, 0, content, 1, bytes.length);
        return this.content;
    }

    @Override
    public int hashCode() {
        return this.value.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof BeLong)) {
            return false;
        }
        BeLong objBL = (BeLong)obj;
        return this.value.equals(objBL.getValue());
    }
    @Override
    public String toString() {
        Long value = this.value;
        return value.toString();
    }
}
